#include "main_user.h"

void main_init(void)
{
	HAL_Delay(500);
	HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1);
	HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_2);
	
	OLED_Init();
	
//   	ds1032_init();
	ds1032_read_realTime();
	
	bsp_su03t_init();
	
	api_hard_init();
	g_hard.light_cold = 100;
	g_hard.light_warm = 100;
	api_key_init();
	api_hard_read();
	api_hard_load();
	g_syspar.temp_gvn = 10;
	api_display_task = api_update_page1;
	HAL_TIM_Base_Start_IT(&htim1);
	HAL_TIM_Base_Start_IT(&htim14);
}

uint16_t i = 0;

void main_user(void)
{
	main_init();
	while(1)
	{
		bsp_su03t_send();
		api_task_cold();
		api_task_save();
		api_task_tab();
		api_task_time();
		api_display_task();
	}
}

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
	bsp_su03t_isr(huart);
}

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
    static uint16_t led_cnt = 0;
	static uint8_t get_temp_flag = 0;
	
    if (htim == (&htim1))
    {
		g_time_ticker++;
		g_get_dis_ticker++;
		g_alarm_beep_ticker++;
		g_alarm_reload_ticker++;
		led_cnt++;
		api_hard_read();
		api_key_scan();
		if(led_cnt >= 500)
		{
			led_cnt = 0;
			g_hard.mcu_led = !g_hard.mcu_led;
		}
		api_hard_load();
    }
	
    if (htim == (&htim14))
    {
		if(!get_temp_flag)
		{
			ds18b20_start_convert();
		}
		else
		{
			g_hard.temp = ds18b20_temp_get();
		}
		get_temp_flag = !get_temp_flag;
    }
}
